package com.icss.mqsp.commitLog;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;

import com.icss.mqsp.servlet.MainServlet;
import com.icss.pangu.logging.Log;
import com.icss.pangu.logging.LogFactory;

public class CommitLog {
	private static Log log = LogFactory.getLog(CommitLog.class);
	private static DataSource dataSource;
	MainServlet mainServlet = new MainServlet();
	public static synchronized DataSource getDatasource() {
		try {
			InitialContext context = new InitialContext();
			dataSource = (DataSource) context.lookup("java:comp/env/jdbc/MQSP");
		} catch (NamingException e) {
			e.printStackTrace();
		}
		return dataSource;
	}

	public boolean saveLog(String uuid,String pid,HttpServletRequest request) {
		PreparedStatement ps = null;
		Connection qspCon = null;
		Connection stmCon = null;
		ResultSet rs = null;
		String ip = getIpAddress(request);
		if (uuid != null && pid != null) {
			try {
				DataSource datasource = CommitLog.getDatasource();
				qspCon = datasource.getConnection();
				Class.forName("com.ibm.db2.jcc.DB2Driver");
				stmCon = DriverManager.getConnection("jdbc:db2://10.3.161.6:50000/stmadc", "db2admin", "db2admin");
				ps = qspCon.prepareStatement("select username,department,name  from mqsp_user where uuid = ?");
				ps.setString(1, uuid);
				if("phl".equals(pid)) {
					pid = "phlapp";
				}
				rs = ps.executeQuery();
				String userCode = null, department = null, userName = null;
				while (rs.next()) {
					userCode = rs.getString("USERNAME");
					department = rs.getString("DEPARTMENT");
					userName = rs.getString("NAME");

				}
				userCode = userCode != null ? userCode : "";
				department = department != null ? department : "";
				userName = userName != null ? userName : "";
				if (!"".equals(userCode + department + userCode)) {
					ps = stmCon.prepareStatement(
							"insert into REPORT_VISIT_COUNT(report_name,person_id,person_name,person_dept,ip,report_count,modify_time,zdtype) values(?,?,?,?,?,1,CURRENT TIMESTAMP,2) ");
					ps.setString(1, pid);
					ps.setString(2, userCode);
					ps.setString(3, userName);
					ps.setString(4, department);
					ps.setString(5, ip);
					boolean flag = ps.execute();
					
					System.out.println(flag);
					return !flag;
				}else {
					return false;
				}
				
			} catch (Exception e) {
				System.out.println("-----------------exception-------------");
				log.info(e);
				e.printStackTrace();
			} finally {
				try {
					if (qspCon != null) {
						qspCon.close();
					}
					if (stmCon != null) {
						stmCon.close();
					}
					if (ps != null) {
						ps.close();
					}
					if (rs != null) {
						rs.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return false;

	}
	public static String getIpAddress(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

}
